package com.deanlib.cabinet.base;

import android.content.Context;

import androidx.databinding.ViewDataBinding;

import com.deanlib.cabinet.ui.widget.dialog.CommonLoadingDialog;
import com.deanlib.plantern.utils.PopupUtils;

/**
 * Created by Dean on 2021/6/19
 */
public class PopupAuxiliary implements IPopupAuxiliary {
    ViewDataBinding binding;
    IActionProtocol actionProtocol;
    CommonLoadingDialog dialog;
    Context context;
    public PopupAuxiliary(ViewDataBinding binding, IActionProtocol actionProtocol){
        this.binding = binding;
        this.context = binding.getRoot().getContext();
        this.actionProtocol = actionProtocol;
    }

    public PopupAuxiliary(Context context){
        this.context = context;
    }

    @Override
    public void toast(String msg) {
        PopupUtils.sendToast(msg);
    }

    @Override
    public void loading(String title, String content) {
        //加载框 引入不闪加载框
        if (dialog == null){
            dialog = new CommonLoadingDialog(context);
        }
        dialog.showSmoothDialog();
    }

    @Override
    public void loaded() {
        dialog.dismissSmoothDialog();
    }

    @Override
    public void networkError() {
        if (binding!=null) {
            //在想要展示提示的页面
            // 1. 加入继承自 ViewGroup 的标签
            // 2. android:id="@+id/nea" (Network Error Area)
            // 3. android:visibility="gone" 或者  android:visibility="invisible"
            /*View nea = binding.getRoot().findViewById(R.id.nea);
            if (nea!=null){
                Observable.create(new ObservableOnSubscribe<View>() {
                    @Override
                    public void subscribe(@NonNull ObservableEmitter<View> emitter) throws Exception {
                        emitter.onNext(nea);
                    }
                }).observeOn(AndroidSchedulers.mainThread()).subscribe(n -> {
                    if (n.getVisibility() != View.VISIBLE) {
                        //两种情况
                        if (n instanceof ViewGroup) {
                            //1. 统一处理
                            ViewGroup groupView = ((ViewGroup) n);
                            if (groupView.getChildCount() == 0) {
                                groupView.addView(LayoutInflater.from(binding.getRoot().getContext()).inflate(R.layout.layout_net_error, groupView, false));
                                Button btnRefresh = binding.getRoot().findViewById(R.id.btnRefresh);
                                if (btnRefresh != null) {
                                    btnRefresh.setOnClickListener(v -> {
                                        groupView.setVisibility(View.GONE);
                                        //刷新 调用 loadData
                                        if (actionProtocol != null) {
                                            actionProtocol.loadData();
                                        }
                                    });
                                }
                            }
                        } else {
                            //2. 自定义处理 直接在xml中自定义界面，并设置 id为 nea 即可
                        }
                        n.setVisibility(View.VISIBLE);
                    }
                });

            }*/
        }
    }

}
